package com.htx.oldbookpavilion.web.authority;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.htx.oldbookpavilion.entity.UserInfo;
import com.htx.oldbookpavilion.common.StandardResponse;
import com.htx.oldbookpavilion.service.authorityservice.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    private ObjectMapper objectMapper = new ObjectMapper();


    /**
     * 校验用户名、手机号和邮箱是否重复
     * @param userInfo
     * @return
     */
    @RequestMapping("/validateUsernameAndPhoneNum")
    public Object validateUsername(UserInfo userInfo){
        StandardResponse<Boolean> sResponse = new StandardResponse<>();
        boolean isValidate = userInfoService.validateUsername(userInfo);
        if (isValidate) {
            sResponse.setSuccess(true);
            sResponse.setMsg("验证成功");
            sResponse.setData(isValidate);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("用户名已存在，请重新填写。");
        }
        return sResponse;
    }

    /**
     * 注册新用户
     * @param params
     * @return
     */
    @RequestMapping("/register")
    public StandardResponse register(@RequestBody Map<String, Object> params) {
        UserInfo userInfo = objectMapper.convertValue(params.get("userInfo"), UserInfo.class);
        boolean normal = objectMapper.convertValue(params.get("normal"), Boolean.class);
        StandardResponse<UserInfo> sResponse = new StandardResponse<>();
        UserInfo newUserInfo = userInfoService.register(userInfo, normal);
        if(newUserInfo == null){
            sResponse.setSuccess(false);
            sResponse.setMsg("注册失败");
        }else{
            sResponse.setSuccess(true);
            sResponse.setMsg("注册成功");
            sResponse.setData(newUserInfo);
        }
        return sResponse;
    }

    /**
     * 用户登录
     * @param userInfo
     * @return
     */
    @RequestMapping("/login")
    public StandardResponse login(@RequestBody UserInfo userInfo, HttpServletRequest request){
        HttpSession session = request.getSession();
        StandardResponse<UserInfo> sResponse = new StandardResponse<>();
        UserInfo loginUser = userInfoService.login(userInfo);
        if (loginUser != null) {
            loginUser.setPassword("");
            sResponse.setSuccess(true);
            sResponse.setData(loginUser);
            sResponse.setMsg("登陆成功");
            if(session.getAttribute("userInfo") != null){
                System.out.println(session.getId());
            }else{
                session.setAttribute("userInfo", loginUser);
                session.setMaxInactiveInterval(60*60*24);
            }
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("登陆失败,用户名或密码错误。");
        }
        return sResponse;
    }

    /**
     * 通过session获取用户信息
     * @param request
     * @return
     */
    @RequestMapping("/getUserInfoBySession")
    public StandardResponse getUserInfoBySession(HttpServletRequest request) {
        StandardResponse<UserInfo> sResponse = new StandardResponse<>();
        HttpSession session = request.getSession();
        if (session.getAttribute("userInfo") != null) {
            sResponse.setSuccess(true);
            sResponse.setMsg("获取用户信息成功");
            sResponse.setData((UserInfo) session.getAttribute("userInfo"));
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("session过期");
        }
        return sResponse;
    }

    /**
     * 用户登出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public StandardResponse logout(HttpServletRequest request) {
        StandardResponse<String> sResponse = new StandardResponse<>();
        HttpSession session = request.getSession();
        if (session.getAttribute("userInfo") != null) {
            session.invalidate();
//            session.removeAttribute("userInfo");
            sResponse.setSuccess(true);
            sResponse.setMsg("登出成功");
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("您还没有登陆。");
        }
        return sResponse;
    }


}
